//
//  JKSelectorView.m
//  JKRecover
//
//  Created by Veer on 2018/6/4.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "JKSelectorView.h"

#define SelectorViewH 260

@interface JKSelectorView() <UIPickerViewDelegate, UIPickerViewDataSource>

// 选择器相关
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UIView *dateSelectView;
@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UILabel *titleLbl;


@end

@implementation JKSelectorView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    // 添加覆盖层
    UIView *maskView = [[UIView alloc] init];
    maskView.backgroundColor = RGB(0, 0, 0);
    maskView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self addSubview:maskView];
    self.maskView = maskView;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(maskViewClick:)];
    [maskView addGestureRecognizer:singleTap];
    
    // 创建dataselect view
    UIView *selectView = [[UIView alloc] init];
    selectView.backgroundColor = [UIColor whiteColor];
    [self addSubview:selectView];
    self.dateSelectView = selectView;
    
    //
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.backgroundColor = [UIColor whiteColor];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.tag = 100;
//    pickerView.showsSelectionIndicator = NO;
    pickerView.frame = CGRectMake(0, 44, kScreenWidth, SelectorViewH - 44);
    [selectView addSubview:pickerView];
    self.pickerView = pickerView;
//    [pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.equalTo(selectView);
//        make.top.mas_equalTo(44);
//    }];
    
    // title
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"住房情况";
    titleLbl.textColor = RGB(53, 53, 53);
    titleLbl.font = VRFont(16);
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [selectView addSubview:titleLbl];
    self.titleLbl = titleLbl;
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(100);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(44);
        make.right.mas_equalTo(-100);
    }];
    
    // 添加取消按钮
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = VRFont(14);
    [cancelBtn setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [selectView addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(onCancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(44);
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    // 添加完成按钮
    UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    finishBtn.titleLabel.font = VRFont(14);
    [finishBtn setTitleColor:RGB(77, 93, 147) forState:UIControlStateNormal];
    [finishBtn setTitleColor:RGB(254, 216, 100) forState:UIControlStateHighlighted];
    [selectView addSubview:finishBtn];
    [finishBtn addTarget:self action:@selector(onFinishBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(44);
    }];
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [selectView addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
}


- (void)show
{
    if (self.superview != nil) return;
    
//    [self.pickerView reloadAllComponents];
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    self.frame = keyWindow.bounds;
    [keyWindow addSubview:self];
    
    [self.pickerView reloadAllComponents];
    
    self.dateSelectView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, SelectorViewH);
    self.maskView.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        
        self.maskView.alpha = 0.3;
        self.dateSelectView.y = kScreenHeight - SelectorViewH;
    }];
}

- (void)dissmiss
{
    [UIView animateWithDuration:0.20 animations:^{
        
        self.maskView.alpha = 0;
        self.dateSelectView.y = kScreenHeight;
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}


#pragma mark - action
- (void)maskViewClick:(UITapGestureRecognizer *)sender
{
    //    CGPoint point = [sender locationInView:self.view];
    
    [self dissmiss];
}

- (void)onCancelBtnClick:(UIButton *)sender
{
    [self dissmiss];
}

- (void)onFinishBtnClick:(UIButton *)sender
{
    
    NSInteger curRow = [self.pickerView selectedRowInComponent:0];
    if (self.sureBlock) {
        
        self.sureBlock(self.titleArray[curRow], curRow);
    }
    [self dissmiss];
}




#pragma mark pickerView代理
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return self.titleArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    return self.titleArray[row];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (0 == component) {

        self.curSelectedIndex = row;
    }
//    if (0 == component) {
//
//        [pickerView reloadComponent:1];
//        [pickerView reloadComponent:2];
//
//        //        [pickerView ];
//        //        [pickerView selectRow:0 inComponent:1 animated:YES];
//        //        [pickerView selectRow:0 inComponent:2 animated:YES];
//    }else if(1 == component){
//
//        [pickerView reloadComponent:2];
//        //        [pickerView selectRow:0 inComponent:2 animated:YES];
//    }else if (2 == component){
//
//
//    }
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:16]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [RGB(255, 255, 255) colorWithAlphaComponent:0.5];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [RGB(255, 255, 255) colorWithAlphaComponent:0.5];
    
    return pickerLabel;
}


#pragma mark - setter && getter
- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    
    [self.pickerView reloadAllComponents];
    
    [self.pickerView selectRow:0 inComponent:0 animated:YES];
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    
    self.titleLbl.text = title;
}

- (void)setCurSelectedIndex:(NSInteger)curSelectedIndex
{
    if (self.titleArray.count > curSelectedIndex) {
        [self.pickerView selectRow:curSelectedIndex inComponent:0 animated:NO];
    }
}

@end
